package com.hyh.web;

import com.hyh.bean.*;
import com.hyh.service.*;
import com.hyh.service.impl.*;
import com.hyh.util.PageUtil;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/role/roles")
public class RolesServlet extends HttpServlet {

    private RoleService roleService = new RoleServiceImpl();
    private MenuService menuService = new MenuServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("selectmenus".equals(method)){
            selectMenus(req,resp);
        }else if ("insert".equals(method)){
            insert(req,resp);
        }else if ("findById".equals(method)){
            findById(req, resp);
        }else if ("update".equals(method)){
            update(req, resp);
        }else if ("delete".equals(method)){
            delete(req, resp);
        }else if ("start".equals(method)){
            start(req, resp);
        }else if ("info".equals(method)){
            info(req, resp);
        }else {
            select(req,resp);
        }
    }

    //查询数据（分页）
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数（分页 + 模糊查询）
        String index = req.getParameter("index");
        int pageIndex = (index == null || index.length() == 0)? 1:Integer.parseInt(index);
        //2.调取service方法（1.查询数据列表的方法 2.查询总条数的方法）
        PageUtil pageUtil = new PageUtil();
        List<Role> usersList = roleService.getRoles(pageIndex, pageUtil.getPageSize());
        int total = roleService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDateList(usersList);
        //3.存值跳转页面
        req.setAttribute("ri",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
    // 查询菜单列表
    protected void selectMenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Menu> menuLsit = menuService.getMenuList();
        req.setAttribute("menulist",menuLsit);
        req.getRequestDispatcher("add.jsp").forward(req,resp);


    }
    // 新增人员
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleName = req.getParameter("rolename");
        String[] menuIds = req.getParameterValues("menuid");
        String state = req.getParameter("state");
        int i = roleService.insertRole(roleName, state, menuIds);
        if (i > 0){
            resp.sendRedirect("/power/role/roles");
        }else {
            resp.sendRedirect("/power/role/roles?method=selectmenus");
        }
    }
    // 主键查询
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        Role byId = roleService.findById(Integer.parseInt(rid));
        List<Menu> menuLsit = menuService.getMenuList();
        req.setAttribute("mlist",menuLsit);
        req.setAttribute("rlist",byId);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
    // 修改角色
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("rid");
        String roleName = req.getParameter("roleName");
        String roleState = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        int i = roleService.updateRole(Integer.parseInt(roleId), roleName, roleState, menuids);
        if (i > 0){
            resp.sendRedirect("/power/role/roles");
        }else{
            resp.sendRedirect("/power/role/roles?method=findById");
        }
    }
    // 删除角色
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        int delete = roleService.delete(Integer.parseInt(rid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (delete > 0){
            writer.println("<script>alert('删除成功');location.href='/power/role/roles';</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/power/role/roles';</script>");
        }
    }
    // 启动状态
    protected void start(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        int start = roleService.start(Integer.parseInt(rid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (start > 0){
            writer.println("<script>alert('启动成功');location.href='/power/role/roles';</script>");
        }else {
            writer.println("<script>alert('启动失败');location.href='/power/role/roles';</script>");
        }
    }
    // 角色详情
    protected void info(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        Role byId = roleService.findById(Integer.parseInt(rid));
        List<Menu> menuLsit = menuService.getMenuList();
        req.setAttribute("mlist",menuLsit);
        req.setAttribute("rlist",byId);
        req.getRequestDispatcher("info.jsp").forward(req,resp);
    }
}
